﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MinesweeperUtility
{
    public class RankList
    {
        public List<Player> rankList { get; private set; }

        public RankList()
        {
            this.rankList = new List<Player>();
        }

        public void AddRanking(Player player)
        {
            this.rankList.Add(player);
            this.rankList = this.rankList.OrderByDescending(x => (int)x.level).ThenBy(y => y.elapsedTime).ToList();
            if (this.rankList.Count > 10)
            {
                this.rankList.RemoveAt(rankList.Count - 1);
            }
            this.SaveRankingList();
        }

        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("\n{0,4} | {1,-16} | {2,10} | {3} \n", "Rank", "Player", "Time Spent", "Level"));
            for (int i = 0; i <= 10 && i < this.rankList.Count; i++)
            {
                sb.Append("\n");
                sb.Append(String.Format("{0,4} | {1,-16} | {2,6} sec | {3} ",
                    i + 1, this.rankList[i].Name, this.rankList[i].elapsedTime / 1000, this.rankList[i].level));
            }
            return sb.ToString();
        }

        public void PrintRankingPosition(Player player)
        {
            int currRank = rankList.IndexOf(player) + 1;
            if (currRank > 10)
            {
                Console.WriteLine("You are out of Top 10");
            }
            else
            {
                Console.WriteLine("Your current position is: {0} ", currRank);
            }
        }


        public static void PrintRankingList(RankList rankList)
        {
            Console.WriteLine(rankList.ToString());
        }

        public void SaveRankingList()
        {
            System.IO.StreamWriter writer = new System.IO.StreamWriter("..\\..\\Top10.txt");
            using (writer)
            {
                writer.WriteLine("\n{0,4} | {1,-16} | {2,10} | {3} \n", "Rank", "Player", "Time Spent", "Level");
                for (int i = 0; i <= 10 && i < this.rankList.Count; i++)
                {
                    writer.WriteLine("{0,4} | {1,-16} | {2,6} sec | {3} ", i + 1, this.rankList[i].Name, this.rankList[i].elapsedTime / 1000, this.rankList[i].level);
                }
            }
        }
        public void LoadRankingList()
        {
            System.IO.StreamReader reader = new System.IO.StreamReader("..\\..\\Top10.txt");
            using (reader)
            {
                string line = String.Empty;
                Level playerLevel;

                line = reader.ReadLine();

                while ((line = reader.ReadLine()) != null)
                {
                    if (line != String.Empty && !line.StartsWith("Rank"))
                    {
                        string[] row = line.Split(new char[] { '|', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                        if (row[4] == "Beginner")
                        {
                            playerLevel = Level.Beginner;
                        }
                        else if (row[4] == "Intermediate")
                        {
                            playerLevel = Level.Intermediate;
                        }
                        else
                        {
                            playerLevel = Level.Expert;
                        }
                        this.rankList.Add(new Player(int.Parse(row[2]) * 1000, row[1], playerLevel));
                    }
                }
            }
        }

    }
}